home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / OLELIB.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  9KB  |  379 lines

  1. /*
  2.  * OLELIB.C
  3.  *
  4.  * Miscellaneous library functions necessary for OLE.  Does not
  5.  * make any OLE calls.
  6.  *
  7.  *  DwReadHuge          Reads potentially > 64K data from a file.
  8.  *  DwWriteHuge         Writes potentially > 64K data to a file
  9.  *  RectConvertMappings Converts a RECT between two logical mapping modes.
  10.  *
  11.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  12.  *
  13.  */
  14.  
  15. #include <windows.h>
  16. #include <ole.h>
  17. #include <commdlg.h>
  18. #include "oclient.h"
  19.  
  20.  
  21. #define CBREADBLOCK 32768
  22.  
  23.  
  24. /*
  25.  * FFileDialog
  26.  *
  27.  * Purpose:
  28.  *  Invokes the COMMDLG.DLL GetOpenFileName dialog and retrieves
  29.  *  a filename for saving or opening.
  30.  *
  31.  * Parameters:
  32.  *  hWnd            HWND of the owning application.
  33.  *  hInst           HANDLE of the application instance.
  34.  *  pszExt          LPSTR of the default extension
  35.  *  pszFilter       LPSTR of the filter desciption.
  36.  *  pszFile         LPSTR buffer to receive the entered filename.
  37.  *                  Must be at least CCHPATHMAX long.
  38.  *  fOpen           BOOL indicating if we want file open or save.
  39.  *
  40.  * Return Value:
  41.  *  BOOL            TRUE if the function retrieved a filename,
  42.  *                  FALSE if the user pressed CANCEL.
  43.  */
  44.  
  45. BOOL FAR PASCAL FFileDialog(HWND hWnd, HANDLE hInst, LPSTR pszExt,
  46.                             LPSTR pszFilter, LPSTR pszFile, LPSTR pszCaption,
  47.                             BOOL fOpen)
  48.     {
  49.     OPENFILENAME    ofn;
  50.     char            szTitle[CCHFILENAMEMAX];
  51.     char            szFilter[80];
  52.     WORD            cch1;
  53.     WORD            cch2;
  54.  
  55.  
  56.     ofn.lStructSize      =sizeof(OPENFILENAME);
  57.     ofn.hwndOwner        =hWnd;
  58.     ofn.hInstance        =hInst;
  59.  
  60.     ofn.lpstrFilter      =szFilter;
  61.     ofn.lpstrCustomFilter=NULL;
  62.     ofn.nMaxCustFilter   =0L;
  63.     ofn.nFilterIndex     =1L;                //We only have 1 extension.
  64.  
  65.     ofn.lpstrFile        =pszFile;
  66.     ofn.nMaxFile         =CCHPATHMAX;
  67.     ofn.lpstrFileTitle   =(LPSTR)szTitle;
  68.     ofn.nMaxFileTitle    =CCHFILENAMEMAX;
  69.  
  70.     ofn.lpstrInitialDir  =NULL;
  71.     ofn.lpstrTitle       =pszCaption;
  72.  
  73.     ofn.Flags            =OFN_HIDEREADONLY;
  74.     ofn.nFileOffset      =0;
  75.     ofn.nFileExtension   =0;
  76.     ofn.lpstrDefExt      =pszExt;
  77.     ofn.lCustData        =NULL;
  78.     ofn.lpfnHook         =NULL;
  79.     ofn.lpTemplateName   =NULL;
  80.  
  81.  
  82.     //Modify the flags as appropriate.
  83.     if (fOpen)
  84.         ofn.Flags        |= OFN_FILEMUSTEXIST;
  85.     else
  86.         ofn.Flags        |= OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
  87.  
  88.  
  89.     //Build a filter like "pszFilter\0*.pszExt\0\0"
  90.     lstrcpy(szFilter, pszFilter);
  91.     cch1=1+lstrlen(szFilter);
  92.  
  93.     cch2=wsprintf(pszFile, "*.%s", pszExt);  //Initial edit control contents.
  94.     lstrcpy(szFilter+cch1, pszFile);         //Append to filter.
  95.  
  96.     //Add the second null-terminator.
  97.     *(szFilter+cch1+cch2+1)=0;
  98.  
  99.     return GetOpenFileName(&ofn);
  100.     }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. /*
  108.  * PszFileFromPath
  109.  *
  110.  * Purpose:
  111.  *  Returns a pointer within an existing pathname string to the
  112.  *  last file of that string.  Used to extract the filename from
  113.  *  a path for use in window titles and message boxes.
  114.  *
  115.  *  Note:  This function does character comparisons to '\' and so
  116.  *  may require more work to localize.
  117.  *
  118.  * Parameters:
  119.  *  pszPath         LPSTR pointer to full pathname.
  120.  *
  121.  * Return Value:
  122.  *  LPSTR           Pointer to a filename within pszPath or NULL if
  123.  *                  there is an error.
  124.  */
  125.  
  126. LPSTR FAR PASCAL PszFileFromPath(LPSTR pszPath)
  127.     {
  128.     LPSTR   psz=NULL;
  129.  
  130.     if (NULL!=pszPath)
  131.         {
  132.         psz=pszPath + lstrlen(pszPath)-1;
  133.  
  134.         while ((psz > pszPath) && (psz[-1]!='\\'))
  135.             psz--;
  136.         }
  137.  
  138.     return psz;
  139.     }
  140.  
  141.  
  142.  
  143. /*
  144.  * PszExtensionFromFile
  145.  *
  146.  * Purpose:
  147.  *  Returns a pointer within an existing filename string to the
  148.  *  extension of that file.  Used to extract the extension from a file
  149.  *  for use in File dialogs and so forth.  The file is scanned backwards
  150.  *  looking for a '.' or '\'.  If no '.' is found before a '\' then
  151.  *  this function returns a pointer to the null terminator.
  152.  *
  153.  *  Note:  This function does character comparisons to '.' and '\' and so
  154.  *  may require more work to localize.
  155.  *
  156.  * Parameters:
  157.  *  pszFile         LPSTR pointer to a filename.
  158.  *
  159.  * Return Value:
  160.  *  LPSTR           Pointer to an extension (starting with the .) within
  161.  *                  pszFile or NULL if there is an error.
  162.  */
  163.  
  164. LPSTR FAR PASCAL PszExtensionFromFile(LPSTR pszFile)
  165.     {
  166.     LPSTR   psz;
  167.     LPSTR   pszT=NULL;
  168.  
  169.     if (NULL!=pszFile)
  170.         {
  171.         //Point to null terminator.
  172.         pszT=pszFile + lstrlen(pszFile);
  173.         psz =pszT-1;
  174.  
  175.         while ((psz > pszFile) && (psz[-1]!='\\') && (psz[-1]!='.'))
  176.             psz--;
  177.  
  178.         if ('.'==psz[-1])
  179.             return --psz;
  180.         }
  181.  
  182.     return pszT;
  183.     }
  184.  
  185.  
  186.  
  187.  
  188.  
  189. /*
  190.  * PszWhiteSpaceScan
  191.  *
  192.  * Purpose:
  193.  *  Skips characters in a string until a whitespace or non-whitespace
  194.  *  character is seen.  Whitespace is defined as \n, \r, \t, or ' '.
  195.  *
  196.  * NOTE:  This function is not extremely well suited to localization.
  197.  *        It assumes that an existing application seeking to become
  198.  *        and OLE client probably already has such a string function
  199.  *        available.
  200.  *
  201.  * Parameters:
  202.  *  psz             LPSTR to string to manipulate
  203.  *  fSkip           BOOL  TRUE if we want to skip whitespace.
  204.  *                  FALSE if we want to skip anything but whitespace.
  205.  *
  206.  * Return Value:
  207.  *  LPSTR           Pointer to first character in the string that either
  208.  *                  non-whitespace (fSkip=TRUE) or whitespace (fSkip=FALSE),
  209.  *                  which may be the null terminator.
  210.  */
  211.  
  212. LPSTR FAR PASCAL PszWhiteSpaceScan(LPSTR psz, BOOL fSkip)
  213.     {
  214.     char        ch;
  215.     BOOL        fWhite;
  216.  
  217.     while (ch=*psz)
  218.         {
  219.         fWhite=('\n'==ch || '\r'==ch || '\t'==ch || ' '==ch);
  220.  
  221.         //Too bad C doesn't have a logical XOR (^^) operator.
  222.         if ((fSkip && !fWhite) || (!fSkip && fWhite))
  223.             break;
  224.  
  225.         psz++;
  226.         }
  227.  
  228.     return psz;
  229.     }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. /*
  240.  * DwReadHuge
  241.  *
  242.  * Purpose:
  243.  *  Provides an _lread function that reads data from a > 64K file
  244.  *  32K at a time until the entire file has been read.  Of course, this
  245.  *  uses huge pointers.
  246.  *
  247.  * Parameters:
  248.  *  hFile       WORD file handle.
  249.  *  pv          LPSTR pointer to data buffer.
  250.  *  dwRead      DWORD bytes to read.
  251.  *
  252.  * Returns:
  253.  *  DWORD       Number of bytes read of 0 if function failed.
  254.  */
  255.  
  256.  
  257. DWORD FAR PASCAL DwReadHuge(WORD hFile, LPVOID pv, DWORD dwRead)
  258.     {
  259.     DWORD     dwT;
  260.     BYTE huge *hp;
  261.  
  262.     dwT=dwRead;
  263.     hp=(BYTE huge *)pv;
  264.  
  265.     while (dwT > (DWORD)CBREADBLOCK)
  266.         {
  267.         if (_lread(hFile, (LPSTR)hp, (WORD)CBREADBLOCK) != CBREADBLOCK)
  268.             return 0L;
  269.  
  270.         dwT-= CBREADBLOCK;
  271.         hp  = hp+(DWORD)CBREADBLOCK;
  272.         }
  273.  
  274.     if (_lread(hFile, (LPSTR)hp, (WORD)dwT) != (WORD)dwT)
  275.         return 0;
  276.  
  277.     return dwRead;
  278.     }
  279.  
  280.  
  281.  
  282.  
  283.  
  284. /*
  285.  * DwWriteHuge
  286.  *
  287.  * Purpose:
  288.  *  Provides an _lwrite function that writes data from a > 64K file
  289.  *  32K at a time until the entire file has been written.  Of course, this
  290.  *  uses huge pointers.
  291.  *
  292.  * Parameters:
  293.  *  hFile       WORD file handle.
  294.  *  pv          LPSTR pointer to data buffer.
  295.  *  dwRead      DWORD bytes to write.
  296.  *
  297.  * Returns:
  298.  *  DWORD       Number of bytes written of 0 if function failed.
  299.  */
  300.  
  301.  
  302. DWORD FAR PASCAL DwWriteHuge(WORD hFile, LPVOID pv, DWORD dwWrite)
  303.     {
  304.     DWORD     dwT;
  305.     BYTE huge *hp;
  306.  
  307.     dwT=dwWrite;
  308.     hp=(BYTE huge *)pv;
  309.  
  310.     while (dwT > (DWORD)CBREADBLOCK)
  311.         {
  312.         if (_lwrite(hFile, (LPSTR)hp, (WORD)CBREADBLOCK) != CBREADBLOCK)
  313.             return 0L;
  314.  
  315.         dwT-= CBREADBLOCK;
  316.         hp  = hp+(DWORD)CBREADBLOCK;
  317.         }
  318.  
  319.     if (_lwrite(hFile, (LPSTR)hp, (WORD)dwT) != (WORD)dwT)
  320.         return 0;
  321.  
  322.     return dwWrite;
  323.     }
  324.  
  325.  
  326.  
  327.  
  328.  
  329. /*
  330.  * RectConvertMappings
  331.  *
  332.  * Purpose:
  333.  *  Converts the contents of a rectangle from one logical mapping
  334.  *  into another.  This function is useful since you have to convert
  335.  *  logical->device then device->logical to do this transformation.
  336.  *
  337.  * Parameters:
  338.  *  pRect           LPRECT containing the rectangle to convert.
  339.  *  mm              WORD mapping mode or source rectangle.
  340.  *
  341.  * Return Value:
  342.  *  None.
  343.  */
  344.  
  345. void FAR PASCAL RectConvertMappings(LPRECT pRect, WORD mmSrc, WORD mmDst)
  346.     {
  347.     HDC      hDC;
  348.     POINT    rgpt[2];
  349.     WORD     mmTemp;
  350.  
  351.     if (NULL==pRect)
  352.         return;
  353.  
  354.     hDC=GetDC(NULL);
  355.  
  356.     rgpt[0].x=pRect->left;
  357.     rgpt[0].y=pRect->top;
  358.     rgpt[1].x=pRect->right;
  359.     rgpt[1].y=pRect->bottom;
  360.  
  361.     //Convert the source units into device units.
  362.     mmTemp=SetMapMode(hDC, mmSrc);
  363.     LPtoDP(hDC, rgpt, 2);
  364.  
  365.     //Convert the device units into the destination units.
  366.     SetMapMode(hDC, mmDst);
  367.     DPtoLP(hDC, rgpt, 2);
  368.  
  369.     pRect->left   =rgpt[0].x;
  370.     pRect->top    =rgpt[0].y;
  371.     pRect->right  =rgpt[1].x;
  372.     pRect->bottom =rgpt[1].y;
  373.  
  374.     //Cleanup and return.
  375.     SetMapMode(hDC, mmTemp);
  376.     ReleaseDC(NULL, hDC);
  377.     return;
  378.     }
  379.